package com.zxjbyte.yiyi.framework.common.core.pdf.support;

import com.itextpdf.text.*;
import com.zxjbyte.yiyi.framework.common.core.pdf.PdfHelper;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Objects;

/**
 * 段落包装器类
 * @Author zhangxingjia
 * @Date 2023/3/25 17:31
 * @Version: 1.0
 */
@Slf4j
public class ParagraphSupport {

    private static final ParagraphSupport instance = new ParagraphSupport();

    public static ParagraphSupport getInstance() {
        return instance;
    }

    private ParagraphSupport(){
    }

    /**
     * 获取标题段落
     * @param title
     * @return
     */
    public Paragraph createTitle(String title){
        Font font = PdfHelper.createFont(15, BaseColor.BLACK);
        Paragraph paragraph = new Paragraph(title, font);
        // 居中
        paragraph.setAlignment(Element.ALIGN_CENTER);
        // 指定段落之前的间距大小
        paragraph.setSpacingBefore(40f);
        return paragraph;
    }

    /**
     * 获取普通文本段落
     * @param text
     * @return
     */
    public Paragraph createText(String text){
        Paragraph paragraph = new Paragraph(text, PdfHelper.createDefaultFont());
        paragraph.setAlignment(Element.ALIGN_MIDDLE);
        // 设置第一行左侧的缩进大小
        paragraph.setFirstLineIndent(20f);
        // 设置文本行的间距
        paragraph.setLeading(13f);
        // 指定段落之前的间距大小
        paragraph.setSpacingBefore(10f);
        return paragraph;
    }

    /**
     * 创建段落
     * @return
     */
    public Paragraph create(){
        Paragraph paragraph = new Paragraph();
        paragraph.setFont(PdfHelper.createDefaultFont());
        paragraph.setAlignment(Element.ALIGN_MIDDLE);
        // 设置第一行左侧的缩进大小
        paragraph.setFirstLineIndent(20f);
        // 设置文本行的间距
        paragraph.setLeading(13f);
        // 指定段落之前的间距大小
        paragraph.setSpacingBefore(10f);
        return paragraph;
    }


    /**
     * 段落内追加
     * @param paragraph
     * @return
     */
    public static ParagraphBuilder builder(Paragraph paragraph) {
        return new ParagraphBuilder(paragraph);
    }

    public static class ParagraphBuilder {

        private final Paragraph paragraph;

        private ParagraphBuilder(Paragraph paragraph) {
            this.paragraph = paragraph;
        }

        /**
         * 追加文本内容
         * @param text
         * @return
         */
        public ParagraphBuilder addText(String text){
            paragraph.add(text);
            return this;
        }

        /**
         * 文本中追加一张图片
         * @param imagePath
         * @return
         */
        public ParagraphBuilder addImage(String imagePath){
            return addImage(imagePath, null, null, null, null);
        }

        /**
         *  文本中追加一张图片
         * @param imagePath 图片路径
         * @param imageWidth 宽度
         * @param imageHeight 高度
         * @param percent 缩放比
         * @param deg 旋转角度
         * @return
         */
        public ParagraphBuilder addImage(String imagePath,
                                         Float imageWidth, Float imageHeight, Float percent, Float deg){
            try {
                Image image = Image.getInstance(imagePath);
                // 设置图像在文档中的对齐方式
                image.setAlignment(Image.DEFAULT);
                // 绘制四个边框
                image.setBorder(Image.BOX);
                // 边框宽度
                image.setBorderWidth(1);
                // 边框颜色
                image.setBorderColor(BaseColor.WHITE);
                if(imageWidth == null || imageHeight == null){
                    // 获取图片大小
                    imageWidth = Math.min(image.getWidth(),
                            PdfHelper.PAGE_SIZE_A4.getWidth() - PdfHelper.DOCUMENT_MARGIN_LEFT - PdfHelper.DOCUMENT_MARGIN_RIGHT);
                    imageHeight = Math.min(image.getHeight(),
                            PdfHelper.PAGE_SIZE_A4.getHeight() - PdfHelper.DOCUMENT_MARGIN_TOP - PdfHelper.DOCUMENT_MARGIN_BOTTOM);
                }
                // 调整图片的大小
                image.scaleToFit(imageWidth, imageHeight);
                // 依照比例缩放
                if(Objects.nonNull(percent)){
                    image.scalePercent(percent);
                }
                // 旋转角度
                if(Objects.nonNull(deg)){
                    image.setRotationDegrees(deg);
                }
                /**
                 * 此方式段落追加图片，图片居文档左侧，后面继续追加的文字，会在图片右下边测起始，自动换行时会换到图片下侧
                 *
                 * 下面方法可以在图片后追加文字为图片右上测，但是如果文字超长自动换行时会覆盖图片上
                 * 笔者暂时未找到解决方式
                 * Chunk chunk = new Chunk(image, 0, -image.getScaledHeight(), true);
                 */
                Chunk chunk = new Chunk(image, 0, 0, true);
                paragraph.add(chunk);
            } catch (BadElementException | IOException ex) {
                log.warn("add image to text fail. [{}]", ex.getMessage());
            }
            return this;
        }

        /**
         * 文本片段内添加超链接
         * @param linkText 例如：点击前往
         * @param link 例如：https://www.baidu.com
         * @return
         */
        public ParagraphBuilder addHyperlink(String linkText, String link) {
            Chunk chunk = new Chunk(linkText);
            chunk.setFont(PdfHelper.createFont(12, BaseColor.BLUE));
            chunk.setAnchor(link);
            // 设置下划线 thickness:下划线的粗细，yPosition:下划线离字的距离
            chunk.setUnderline(0.1f, -2f);
            paragraph.add(chunk);
            return this;
        }

        /**
         * 换行 \n 的效果
         * @return
         */
        public ParagraphBuilder slashN(){
            paragraph.add(Chunk.NEWLINE);
            return this;
        }

        /**
         * 添加制表符
         * @return
         */
        public ParagraphBuilder tab(){
            paragraph.add(Chunk.TABBING);
            return this;
        }

        /**
         * 创建A锚点
         * @param aTagText
         * @param flag
         * @return
         */
        public ParagraphBuilder aTag(String aTagText, String flag){
            // 点击锚点
            Chunk anchorPoint = new Chunk(aTagText);
            anchorPoint.setLocalGoto(flag);
            anchorPoint.setFont(PdfHelper.createFont(12, BaseColor.GRAY));
            paragraph.add(anchorPoint);
            return this;
        }

        /**
         * 创建B锚点  实现A -> B
         *  注意：锚点A和锚点B flag标记一致才能实现跳转
         * @param bTagText
         * @param flag
         * @return
         */
        public ParagraphBuilder bTag(String bTagText, String flag){
            // 跳转到锚点
            Chunk anchorPoint = new Chunk(bTagText);
            anchorPoint.setLocalDestination(flag);
            anchorPoint.setFont(PdfHelper.createFont(12, BaseColor.GRAY));
            paragraph.add(anchorPoint);
            return this;
        }

        public Paragraph build(){
            return paragraph;
        }
    }
}
